<template>
  <base-page-view class-name="inventory-page">
    <div v-for="(item, index) in list" :key="index" class="inventory-list">
      <div class="image-box">
        <b-image class-name="main-pic" :src="item.box_series.img" mode="aspectFill" />
        <view class="stock">库存  {{ item.stock_number }} {{ item.stock_unit }}</view>
      </div>
      <view class="main-text">
        <view class="text">{{ item.box_series.series_name }}</view>
        <view class="details" @click="$navigateTo(`/pagesBox/inventory-details/index?series_id=${item.box_series.series_id}&stock_id=${(item.box_stock_goods_v_o_s[0]?item.box_stock_goods_v_o_s[0].stock_id:-1)}`)">
          <text class="details-text">出入库明细</text>
          <image class="details-icon" src="/static/images/inventory/arrow_left.png" mode="aspectFit" />
        </view>
      </view>
      <view class="btn-line">
        <view class="btn-item" :class="{ disabled: !item.can_allocate }" @click="operate('release', item)">
          <b-image class-name="icon" src="/static/images/box/icon_release.png" mode="aspectFit" :is-asset="true" />
          <view>库存下拨</view>
          <!-- <text v-if="item.to_approval_number" class="num">{{ item.to_approval_number > 99 ? '99+' : item.to_approval_number }}</text> -->
        </view>
        <view class="btn-item" :class="{ disabled: !item.can_change }" @click="operate('transform', item)">
          <b-image class-name="icon" src="/static/images/box/icon_transform.png" mode="aspectFit" :is-asset="true" />
          <view>库存转换</view>
        </view>
        <view class="btn-item" :class="{ disabled: !item.stock_number }" @click="operate('get', item)">
          <b-image class-name="icon" src="/static/images/box/icon_get.png" mode="aspectFit" :is-asset="true" />
          <view>立即提货</view>
        </view>
      </view>
      <!-- <view class="btn-box">
        <view v-if="item.box_stock_goods_v_o_s[0]" class="long-btn" @click="$navigateTo(`/pagesBox/gift-bag/index?goods_id=${item.box_stock_goods_v_o_s[0].goods_id}&sku_code=${item.box_stock_goods_v_o_s[0].sku_code}&stock_id=${item.box_stock_goods_v_o_s[0].stock_id}`)">立即提货(<text class="bold">{{ item.box_stock_goods_v_o_s[0]?item.box_stock_goods_v_o_s[0].stock_number:0 }}</text>箱)</view>
        <view v-else class="long-btn gray-btn">立即提货(<text class="bold">{{ item.box_stock_goods_v_o_s[0]?item.box_stock_goods_v_o_s[0].stock_number:0 }}</text>箱)</view>
      </view>
      <view v-if="permissionList.indexOf(3) !== -1" class="details">
        <text class="details-text" @click="$navigateTo('/pagesBox/approval/index?tab=1')">待审批<text class="num">{{ item.box_stock_goods_v_o_s[0]?item.box_stock_goods_v_o_s[0].to_approval_number:0 }}</text>条</text>
        <image class="details-icon" src="/static/images/inventory/arrow_left.png" mode="aspectFit" @click="$navigateTo('/pagesBox/approval/index?tab=1')" />
      </view> -->
    </div>
  </base-page-view>
</template>
<script>
import './index.scss'
export default {
  name: 'Inventory',
  data() {
    return {
      list: [],
      permissionList: []
    }
  },
  onLoad() {
    // this.getPermission()
    // this.getList()
  },
  onShow() {
    this.getPermission()
    this.getList()
  },
  methods: {
    async getList() {
      const res = await this.$api.mineApi.inventoryList()
      if (res.success) {
        const list = res.data || []
        this.list = list.map(i => ({
          ...i,
          stock_unit: i.box_stock_goods_v_o_s.length ? i.box_stock_goods_v_o_s[0].stock_unit : (i.box_series ? i.box_series.stock_unit : ''),
          stock_number: i.box_stock_goods_v_o_s.length ? i.box_stock_goods_v_o_s[0].stock_number : 0,
          to_approval_number: i.box_stock_goods_v_o_s.length ? i.box_stock_goods_v_o_s[0].to_approval_number : 0
        }))
      }
    },
    async getPermission() {
      const res = await this.$api.mineApi.getPermission()
      if (res.success) {
        this.permissionList = res.data
      }
    },
    async operate(type, item) {
      switch (type) {
        case 'release':
          if (item.box_stock_goods_v_o_s.length && item.can_allocate) {
            this.$navigateTo(`/pagesBox/inventory/release?goods_id=${item.box_stock_goods_v_o_s[0].goods_id}&sku_code=${item.box_stock_goods_v_o_s[0].sku_code}`)
          }
          break
        case 'transform':
          if (item.stock_number && item.box_series && item.can_change) {
            this.$navigateTo(`/pagesBox/inventory/transform?series_id=${item.box_series.series_id}`)
          }
          break
        default:
          if (item.stock_number && item.box_series) {
            this.$navigateTo(`/pagesBox/gift-bag/index?goods_id=${item.box_stock_goods_v_o_s[0].goods_id}&sku_code=${item.box_stock_goods_v_o_s[0].sku_code}&stock_id=${item.box_stock_goods_v_o_s[0].stock_id}&series_id=${item.box_series.series_id}`)
          }
      }
    }
  }
}
</script>
